import appengine_utilities.sessions
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template
import model.User as User
import os


class BBPage(webapp.RequestHandler):
    def outputUnauthorizedHeader(self):
        path = os.path.join(os.path.dirname(__file__), 'unauthorized_header.html')
        self.response.out.write(template.render(path, {}))
    
    def outputHeader(self):
        self.outputUnauthorizedHeader()

class AuthorizedPage(BBPage):
    def __init__(self):
        self.authorizedUser = None
    
    def get(self):
        session = appengine_utilities.sessions.Session()
        if session.has_key("username"):
            self.authorizedUser = User.getUserByUsername(session["username"])    
            self.getIfAuthorized()
        else:
            self.authorizedUser = None
            self.redirect("/")
            
    def getIfAuthorized(self):
        pass

    def post(self):
        session = appengine_utilities.sessions.Session()
        if session.has_key("username"):
            self.authorizedUser = User.getUserByUsername(session["username"])    
            self.postIfAuthorized()
        else:
            self.authorizedUser = None
            self.redirect("/")
            
    def postIfAuthorized(self):
        pass
    
    def outputHeader(self):
        self.outputAuthorizedHeader()
    
    def outputAuthorizedHeader(self):
        template_values = {"username":self.authorizedUser.username}
        path = os.path.join(os.path.dirname(__file__), 'header.html')
        self.response.out.write(template.render(path, template_values))
      